//
//  DataPageViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/7.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class DataPageViewController: BaseViewController,beginRefresh {

    var index : Int = 1 //用于判断datapage的类型
//    var pageIndex : Int = 1 //用于判断当前属于第几页
    var listView : DataListView?
    var isHight : Bool = false
    var noDataView : NoDataView?
    weak var alertCtl : BaseViewController?
    override func viewDidLoad() {
        super.viewDidLoad()
        if isHight == false {
            listView = DataListView.init(frame: CGRect.init(origin: self.view.frame.origin, size: CGSize.init(width: YXDeviceWidth, height: YXDeviceHeight - 90 * YXHeightScale - 64 - 49)), delegate: self)
            noDataView = NoDataView.init(frame: CGRect.init(origin: self.view.frame.origin, size: CGSize.init(width: YXDeviceWidth, height: YXDeviceHeight - 90 * YXHeightScale - 64 - 49)))
            noDataView?.backgroundColor = UIColor.white
        } else {
            listView = DataListView.init(frame: CGRect.init(origin: self.view.frame.origin, size: CGSize.init(width: YXDeviceWidth, height: YXDeviceHeight - 90 * YXHeightScale - 64)), delegate: self)
            noDataView = NoDataView.init(frame: CGRect.init(origin: self.view.frame.origin, size: CGSize.init(width: YXDeviceWidth, height: YXDeviceHeight - 90 * YXHeightScale - 64)))
            noDataView?.backgroundColor = UIColor.white
        }
        noDataView?.refreshBlock = {
            self.refresh()
        }
        self.view.addSubview(listView!)
        listView?.isHidden = true
        self.view.addSubview(noDataView!)
        noDataView?.isHidden = true
        loadData(idx: index)
        NotificationCenter.default.addObserver(self, selector: #selector(refresh), name: NSNotification.Name("addComplete"), object: nil)
    }
    
    @objc func refresh() -> Void {
        loadData(idx: index)
    }

    func loadData(idx : Int) -> Void {
        weak var sslf = self
        Manager.shared().getData(kind: idx, success: {(model) in
            if let mod = model as? TurnOverModel {
                if mod.dataSrc.count > 0 {
                    sslf?.listView?.isHidden = false
                    sslf?.noDataView?.isHidden = true
                    sslf?.listView!.handleData(model: mod)
                } else {
                    sslf?.noDataView?.isHidden = false
                    sslf?.listView?.isHidden = true
                    sslf?.noDataView?.endRefresh()
                }
            }
        }, fail: {(error) in
            sslf?.noDataView?.endRefresh()
            sslf?.alertCtl?.endAnim()
            sslf?.alertCtl?.commonAlertMsg(content: YXNetWorkFail)
        })
    }
    
    //加载更多代理方法
    func beginRefresh() {
        loadData(idx: index)
    }

    func loadNext() {
//        pageIndex = pageIndex + 1
//        listView?.startAppend()
//        loadData(idx: pageIndex)
    }
    
    deinit {
        print("datapage deinit")
    }
  

}
